# smtplib 用于邮件的发信动作
import smtplib
from email.mime.text import MIMEText
# email 用于构建邮件内容
from email.header import Header
import socket
import getpass
import pandas as pd
import datetime


def sendEmail(title, body, fromAddress='yehb@qq.com', toAddress='amstelveen@dingtalk.com'):
    # 用于构建邮件头

    # 发信方的信息：发信邮箱，QQ 邮箱授权码
    # yehb@qq.com
    from_addr = fromAddress
    password = 'pjotakumnnhobjja'

    # 收信方邮箱
    # amstelveen@dingtalk.com
    to_addr = toAddress

    # 发信服务器
    smtp_server = 'smtp.qq.com'

    # 邮箱正文内容，第一个参数为内容，第二个参数为格式(plain 为纯文本)，第三个参数为编码
    msg = MIMEText(body, 'plain', 'utf-8')

    # 邮件头信息
    msg['From'] = Header(from_addr)
    msg['To'] = Header(to_addr)
    msg['Subject'] = Header(title)

    # 开启发信服务，这里使用的是加密传输
    server = smtplib.SMTP_SSL(host=smtp_server)
    server.connect(smtp_server, 465)
    # 登录发信邮箱
    server.login(from_addr, password)
    # 发送邮件
    server.sendmail(from_addr, to_addr, msg.as_string())
    # 关闭服务器
    server.quit()
    # print("发送了一封邮件")


def sendAlert(title, body, engine, conn):
    user_name = getpass.getuser()  # 获取当前用户名
    hostname = socket.gethostname()  # 获取当前主机名
    chineseName = ''

    receiveEmailAddress = ''
    machineDesc = ''

    sqlLoop = "SELECT * FROM 平台_物理机虚拟机 where 机器名称='" + hostname + "' "
    dfLoop = pd.read_sql(sql=sqlLoop, con=engine)
    for index, row in dfLoop.iterrows():
        receiveEmailAddress = row['责任人告警邮箱']
        chineseName = row['机器责任人']
        machineDesc = row['机器描述']

    sendEmail(title + '|' + datetime.datetime.now().strftime('%m-%d %H:%M:%S'),
              body + '| 报错机器:' + hostname + '|机器责任人:' + chineseName + '|☆:' + machineDesc,
              fromAddress='yehb@qq.com',
              toAddress=receiveEmailAddress)
    print(title + '|' + datetime.datetime.now().strftime(
        '%m-%d %H:%M:%S') + body + '| 报错机器:' + hostname + '|机器责任人:' + chineseName + '|☆:' + machineDesc)


def sendFinalSuccessEmail(title, body, engine, conn, modeName, runMode):
    user_name = getpass.getuser()  # 获取当前用户名
    hostname = socket.gethostname()  # 获取当前主机名
    chineseName = ''

    receiveEmailAddress = ''
    machineDesc = ''

    sqlLoop = "SELECT * FROM 平台_物理机虚拟机 where 机器名称='" + hostname + "' "
    dfLoop = pd.read_sql(sql=sqlLoop, con=engine)
    for index, row in dfLoop.iterrows():
        receiveEmailAddress = row['责任人告警邮箱']
        chineseName = row['机器责任人']
        machineDesc = row['机器描述']

    sendEmail(title + '|' + datetime.datetime.now().strftime('%m-%d %H:%M:%S'),
              body + '| 成功机器:' + hostname + '|机器责任人:' + chineseName + '|☆:' + machineDesc,
              fromAddress='yehb@qq.com',
              toAddress=receiveEmailAddress)
    print(title + '|' + datetime.datetime.now().strftime(
        '%m-%d %H:%M:%S') + body + '| 成功机器:' + hostname + '|机器责任人:' + chineseName + '|☆:' + machineDesc)

    # 除开发送邮件，我们还需要更新一个数据库的标志，这样更容易进行统计
    sql = "update 平台_任务_机器_实例表 set 执行结果='已完成' "
    sql += " where 任务表名称='" + modeName + "' and 当前运行机器名='" + hostname + "' and 运行模式='" + runMode + "' "
    engine.execute(sql)


if __name__ == '__main__':
    sendEmail('昨天邮件无法发送', '昨天邮件无法发送，看看是否是接口问题')
